package my.diplom.dao.impl;

import my.diplom.dao.IEventDAO;
import my.diplom.entity.Event;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.orm.hibernate3.HibernateTemplate;

/**
 * Created by IntelliJ IDEA.
 * User: gb
 * Date: 14.07.2009
 * Time: 15:28:11
 * To change this template use File | Settings | File Templates.
 */
public class EventDAOImpl implements IEventDAO {

    private static final Logger log = Logger.getLogger(EventDAOImpl.class);

    private HibernateTemplate template;

    public void setTemplate(HibernateTemplate template) {
        this.template = template;
    }

    public void create(Event event) {
        log.debug("create event" + event.getDescription());
        template.save(event);
    }

    public void update(Event event) {
        log.debug("update event" + event.getDescription());
        template.saveOrUpdate(event);
    }

    public Event getById(Long eventId) {
        log.debug("get event by id = " + eventId);
        return (Event) template.get(Event.class, eventId);
    }

    public List<Event> getAll() {
        log.debug("get all events");
         //noinspection JpaQlInspection
        return template.find("from Event order by startDate ");
    }

    public void delete(Event event) {
        log.debug("delete event" + event.getDescription());
        template.delete(event);
    }

    @Override
    public List<Event> getEventsByLogin(String login) {
        return template.find("from Event e where e.user.login = ? order by startDate ", login);
    }
}
